package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.DateTimeUtil;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.entity.JxComputyDeviceEntity;
import io.renren.modules.sys.entity.JxStudentScheduleEntity;
import io.renren.modules.sys.service.JxComputyDeviceService;
import io.renren.modules.sys.service.JxStudentScheduleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.JxCourseScheduleEntity;
import io.renren.modules.sys.service.JxCourseScheduleService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 课程表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-10-16 13:42:19
 */
@RestController
@RequestMapping("sys/jxcourseschedule")
public class JxCourseScheduleController {
    @Autowired
    private JxCourseScheduleService jxCourseScheduleService;

    @Autowired
    private JxStudentScheduleService  jxStudentScheduleService;
    @Autowired
    private JxComputyDeviceService  jxComputyDeviceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:jxcourseschedule:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jxCourseScheduleService.getAllWithPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:jxcourseschedule:info")
    public R info(@PathVariable("id") Long id){
        JxCourseScheduleEntity jxCourseSchedule = jxCourseScheduleService.getById(id);

        return R.ok().put("jxCourseSchedule", jxCourseSchedule);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:jxcourseschedule:save")
    public R save(@RequestBody JxCourseScheduleEntity jxCourseSchedule){
        ValidatorUtils.validateEntity(jxCourseSchedule,AddGroup.class);
        jxCourseSchedule.setUsageRate(0.00);
        if(!DateTimeUtil.compareTime(jxCourseSchedule.getEndTime(),jxCourseSchedule.getBegainTime())){
         return R.error("开始时间需要大于结束时间");
        }
        String msg=jxCourseScheduleService.ValidatorTimeStage(jxCourseSchedule.getBegainTime(),jxCourseSchedule.getEndTime(),jxCourseSchedule);
        if(msg.length()>0){
            return R.error(msg);
        }
        jxCourseSchedule.setCourseDay(DateTimeUtil.date(jxCourseSchedule.getBegainTime()));
        jxCourseSchedule.setTimeStage(DateTimeUtil.TimeQuantum(jxCourseSchedule.getBegainTime(),jxCourseSchedule.getEndTime()));
        jxCourseScheduleService.save(jxCourseSchedule);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:jxcourseschedule:update")
    public R update(@RequestBody JxCourseScheduleEntity jxCourseSchedule){
        ValidatorUtils.validateEntity(jxCourseSchedule, UpdateGroup.class);
        jxCourseSchedule.setCourseDay(DateTimeUtil.date(jxCourseSchedule.getBegainTime()));
        jxCourseScheduleService.updateById(jxCourseSchedule);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:jxcourseschedule:delete")
    public R delete(@RequestBody Long[] ids){
        jxCourseScheduleService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 拿到允许调配的实验室
     * @param id
     * @return
     */
    @RequestMapping("/mixLaboratory/{id}")
    public  R  getmixLaboratory(@PathVariable("id") Long id){
       JxCourseScheduleEntity  entity= jxCourseScheduleService.getById(id);
       Map<String,Object> params=new HashMap<String,Object>();
       params.put("courseId",entity.getCourseId());
       params.put("teacherId",entity.getTeacherId());
       params.put("courseDay",entity.getCourseDay());
       params.put("batch",entity.getBatch());
       params.put("paramsSql","a.usage_rate  < 1 ");
        List<JxCourseScheduleEntity> list=jxCourseScheduleService.getList(params);
        return  R.ok().put("data",list);

    }

    /**
     * 拿到允许调配的设备
     * @param id   当前实验室的courseScheduleId
     * @return
     */
    @RequestMapping("/mixDevices/{id}")
    public  R  getmixDevices(@PathVariable("id") Long id){
        JxCourseScheduleEntity  entity= jxCourseScheduleService.getById(id);
        Long  laboratoryId=entity.getLaboratoryId();  // 实验室的id
        Map<String,Object> params=new HashMap<String,Object>();
        params.put("ids",id);
        List<JxStudentScheduleEntity>  studentSchedules=jxStudentScheduleService.allSelectDevicesId(params);
        params.put("ids", 0);
        if (studentSchedules.size() > 0) {
            String ids = "";
            for (int i = 0; i < studentSchedules.size(); i++) {
                if (i == 0) {
                    ids = ids + studentSchedules.get(0).getUseDeviceId();
                } else {
                    ids = ids + "," + studentSchedules.get(i).getUseDeviceId();
                }
            }
            params.put("ids", ids);
        }
        params.put("laboratoryId",laboratoryId);
        List<JxComputyDeviceEntity> computyDevices=jxComputyDeviceService.allMixDevice(params);
        return  R.ok().put("data",computyDevices);

    }

}
